package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.RoleMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Role;
import com.itheima.service.CheckGroupService;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service(interfaceClass = RoleService.class)
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    /**
     * 新增检查组
     * @param
     * @param
     */
    @Override
    public void add(Integer[] menus, Role role,Integer[] permissions) {

        //先新增检查组
        roleMapper.add(role);

        //添加用户和菜单的中间表关系
        setCheckGroupAndCheckItem(menus,role.getId());

        //添加用户和权限的中间表关系
        setRoleAndPermission(permissions,role.getId());

    }
    //添加检查组和检查项的中间表关系
    private void setCheckGroupAndCheckItem(Integer[] menus, Integer id) {

        if (menus!=null && menus.length>0){
            for (Integer menu : menus) {

                roleMapper.setCheckGroupAndCheckItem(menu,id);
            }
        }

    }
    //添加检查组和权限的中间表关系
    private void setRoleAndPermission(Integer[] permissions, Integer id) {

        if (permissions!=null && permissions.length>0){
            for (Integer permission : permissions) {

                roleMapper.setRoleAndPermission(permission,id);
            }
        }

    }


    /*private void setCheckGroupAndCheckItem(Integer[] checkItemIds, Integer id) {

        if (checkItemIds!=null && checkItemIds.length>0){
            for (Integer checkItemId : checkItemIds) {
                checkGroupMapper.setCheckGroupAndCheckItem(checkItemId,id);
            }
        }

    }*/

    /**
     * 查询检查组的分页数据
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {

        PageHelper.startPage(currentPage,pageSize);

        //条件查询数据
        List<Role> checkGroupList = roleMapper.findByCondition(queryString);

        //数据包装
        PageInfo<Role> pageInfo = new PageInfo<>(checkGroupList);

        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 通过id查到检查组
     */
    @Override
    public Role findById(Integer id) {
        return roleMapper.findById(id);
    }

    /**
     * 编辑更新
     * @param
     * @param
     */
    @Override
    public void edit(Integer[] menus, Role role,Integer[] permissions) {

        //删除中间表关系
        roleMapper.deleteAssociation(role.getId());

        roleMapper.deletePermission(role.getId());

        //重新设置中间表关系
        setCheckGroupAndCheckItem(menus,role.getId());

        setRoleAndPermission(permissions,role.getId());

        //更新检查组
        roleMapper.edit(role);


    }

    /**
     * 通过id删除检查组
     * @param id
     */
    @Override
    public void deleteById(Integer id) {


        //判断检查组有没有被套餐引用
        Integer count = roleMapper.findCountById(id);
        if (count>0){
            throw new RuntimeException(MessageConstant.CHECKGROUP_IS_QUOTED);
        }

        //删除检查组和检查项的中间表关系
        roleMapper.deleteAssociation(id);


        roleMapper.deletePermission(id);



        //删除检查组
        roleMapper.deleteById(id);


    }

    /**
     * 查询所有检查组
     */
    @Override
    public List<Role> findAll() {
        return roleMapper.findAll();
    }

    /**
     * 通过检查组id查询检查项的id集合
     * @param id
     * @return
     */
    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {

        return roleMapper.findCheckItemIdsByCheckGroupId(id);
    }
}
